import { http } from "@/utils/http";

// 通用响应类型
type Result = {
  code: number;
  message: string;
  data?: any;
  timestamp: string;
};

// 会员套餐数据类型
export type MemberPackage = {
  id: number;
  name: string; // 套餐名称
  description?: string; // 套餐描述
  price: number; // 套餐价格
  durationDays: number; // 套餐时长（天）
  level: number; // 套餐等级
  isActive: boolean; // 是否启用
  createdAt: string;
  updatedAt: string;
};

// 创建会员套餐参数
export type CreateMemberPackageParams = {
  name: string;
  description?: string;
  price: number;
  durationDays: number;
  level?: number;
  isActive?: boolean;
};

// 更新会员套餐参数
export type UpdateMemberPackageParams = Partial<CreateMemberPackageParams>;

// 分页查询参数
export type MemberPackagePageParams = {
  page?: number;
  limit?: number; // 每页数量
  includeInactive?: boolean; // 是否包含非活跃套餐
  isActive?: boolean; // 启用状态筛选
};

// 分页响应
export type MemberPackagePageResponse = {
  data: MemberPackage[];
  total: number;
  page: number;
  limit: number;
  totalPages: number;
};

/**
 * 创建会员套餐
 * POST /api/member/packages
 * 接口ID：323791200
 */
export const createMemberPackage = (data: CreateMemberPackageParams) => {
  return http.request<Result>("post", "/api/member/packages", { data });
};

/**
 * 获取所有启用的会员套餐
 * GET /api/member/packages
 * 接口ID：323791201
 */
export const getEnabledMemberPackages = () => {
  return http.request<Result & { data: MemberPackage[] }>(
    "get",
    "/api/member/packages"
  );
};

/**
 * 管理员获取所有会员套餐
 * GET /api/member/packages/admin
 * 接口ID：323791202
 */
export const getAllMemberPackages = () => {
  return http.request<Result & { data: MemberPackage[] }>(
    "get",
    "/api/member/packages/admin"
  );
};

/**
 * 分页获取会员套餐
 * GET /api/member/packages/page
 * 接口ID：323791203
 */
export const getMemberPackagesPage = (params?: MemberPackagePageParams) => {
  return http.request<Result & { data: MemberPackagePageResponse }>(
    "get",
    "/api/member/packages/page",
    { params }
  );
};

/**
 * 根据ID获取会员套餐
 * GET /api/member/packages/{id}
 * 接口ID：323791204
 */
export const getMemberPackageById = (id: number) => {
  return http.request<Result & { data: MemberPackage }>(
    "get",
    `/api/member/packages/${id}`
  );
};

/**
 * 更新会员套餐
 * PUT /api/member/packages/{id}
 */
export const updateMemberPackage = (
  id: number,
  data: UpdateMemberPackageParams
) => {
  return http.request<Result>("patch", `/api/member/packages/${id}`, { data });
};

/**
 * 删除会员套餐
 * DELETE /api/member/packages/{id}
 */
export const deleteMemberPackage = (id: number) => {
  return http.request<Result>("delete", `/api/member/packages/${id}`);
};

/**
 * 切换会员套餐启用状态
 * PATCH /api/member/packages/{id}/toggle
 */
export const toggleMemberPackageStatus = (id: number) => {
  return http.request<Result>("patch", `/api/member/packages/${id}/toggle`);
};
